<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false" width="800px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">

      <el-row :gutter="20">
        <el-col :span="12">
          <div class="tmplmsg__detail-showcase">
            <div class="tmplmsg__showcase">
              <div class="tmplmsg__showcase-wrap" v-html="exampleDiv">
                <!--                {{exampleDiv}}-->
              </div>
              <div class="tmplmsg__showcase-link">详情</div>
            </div>
            <div class="tmplmsg__showcase-notice">{{$t('wechat.contentExample')}}</div>
          </div>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('wechat.title')" prop="title">
            <el-tag type="info">{{dataForm.title}}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('wechat.industry')" prop="primaryIndustry">
            <el-tag type="info">{{dataForm.primaryIndustry}}-{{dataForm.deputyIndustry}}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('wechat.typeTitle')" prop="type">

            <el-select v-if="dataForm.status==0" v-model="dataForm.type" :placeholder="$t('wechat.typeTitle')">
              <el-option v-for="template in this.$getDictList(this.TEMPLATE_TYPE, false)" :key="template.val"
                :label="template.label" :value="template.val"></el-option>
            </el-select>
            <el-tag v-else type="info">{{ $getDictLabel(TEMPLATE_TYPE, dataForm.type) }}</el-tag>
          </el-form-item>

          <el-form-item :label="$t('wechat.url')" prop="url">
            <el-input v-model="dataForm.url" :placeholder="$t('wechat.urlPlaceholder')"></el-input>
          </el-form-item>

          <el-form-item :label="$t('wechat.miniprogramAppid')" prop="miniappAppid">
            <el-input v-model="dataForm.miniappAppid" :placeholder="$t('wechat.miniprogramAppidPlaceholder')">
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('wechat.miniprogramPagepath')" prop="miniappPagepath">
            <el-input v-model="dataForm.miniappPagepath" :placeholder="$t('wechat.miniprogramPagepathPlaceholder')">
            </el-input>
          </el-form-item>

          <el-form-item :label="item.label" v-for="(item,index) in dataForm.propertys" :key="index">
            <el-row>
              <el-col :span="16">
                <el-input v-model="item.value"></el-input>
              </el-col>
              <el-col :span="8">
                <el-color-picker v-model="item.color"></el-color-picker>
              </el-col>
            </el-row>
          </el-form-item>

        </el-col>
      </el-row>

    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<style scoped>
.tmplmsg__detail-showcase {
  float: left;
  margin-right: 70px;
  line-height: 1.6;
  font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue',
    'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei',
    Arial, sans-serif;
  color: #353535;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

.tmplmsg__showcase {
  width: 320px;
  border: 1px solid #e4e8eb;
}

.tmplmsg__showcase-wrap {
  padding: 18px 20px;
}

.tmplmsg__showcase-link {
  padding: 0 20px;
  border-top: 1px solid #e4e8eb;
  line-height: 44px;
  position: relative;
  color: #9a9a9a;
}

.tmplmsg__showcase-notice {
  text-align: center;
  color: #9a9a9a;
  padding-top: 30px;
}
</style>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      TEMPLATE_TYPE: 'WECHAT_TEMPLATE',
      dataForm: {
        id: '', templateId: '', title: '', primaryIndustry: '', deputyIndustry: '', content: '', example: '', creator: '', createDate: '', propertys: [], status: 0, url: '', miniappAppid: '', miniappPagepath: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        type: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    },
    exampleDiv () {
      return this.dataForm.example.replace(/\n/g, '<br/>')
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/wechat/template/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/wechat/template/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
